package com.example.kermit.zhpaper.ui.Fragment;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.example.kermit.zhpaper.R;
import com.example.kermit.zhpaper.entity.NewsContent;
import com.example.kermit.zhpaper.http.MySingleRequestQueue;
import com.google.gson.Gson;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Kermit on 15-7-29.
 * e-mail : wk19951231@163.com
 */
public class ContentFragment extends Fragment {

    private static final String LOG_TAG = "ContentFragment";

    @Bind(R.id.id_news_webview)
    WebView webView;

    NewsContent content;
    MySingleRequestQueue singleRequestQueue;
    private ContentFragmentListener listener;

    public interface ContentFragmentListener{
        void contentFragmentCallback();
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        listener = (ContentFragmentListener) activity;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.content_layout, container, false);
        ButterKnife.bind(this, view);

        String newsUrl = getArguments().getString("newsUrl");

        Log.i(LOG_TAG, newsUrl);
        StringRequest newsContentRequest = new StringRequest(newsUrl,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(@NonNull String response) {
                        Gson gson = new Gson();
                        content = gson.fromJson(response, NewsContent.class);
                        String body = content.getBody();
                        String css = content.getCss().get(0);
                        String imageUrl = content.getImage();
                        String title = content.getTitle();
                        String image_source = content.getImage_source();

                        StringBuilder stringBuilder = new StringBuilder();
                        stringBuilder.append("<div class=\"img-wrap\">")
                                .append("<h1 class=\"headline-title\">")
                                .append(title)
                                .append("</h1>")
                                .append("<span class=\"img-source\">")
                                .append("图片：")
                                .append(image_source)
                                .append("</span>")
                                .append("<img src=")
                                .append(imageUrl)
                                .append(" alt=\"\"></div>");

                        body = body.replace("<div class=\"img-place-holder\"></div>", stringBuilder.toString());
                        Log.i(LOG_TAG, stringBuilder.toString());
                        String cssLink = "<LINK href="+ css + " type=\"text/css\" rel=\"stylesheet\"/>";
                        body = "<html><body>" + cssLink + body + "</body></html>";
                        String encoding = "utf-8";
                        String mime = "text/html";
                        Log.i(LOG_TAG, body);
                        webView.getSettings().setJavaScriptEnabled(true);
                        webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
                        webView.getSettings().setDatabaseEnabled(true);
                        webView.getSettings().setAppCacheEnabled(true);
                        webView.loadDataWithBaseURL(null, body, mime, encoding, null);
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {

                    }
                }
        );
        singleRequestQueue = MySingleRequestQueue.getInstance(getActivity());
        singleRequestQueue.addtoRequestQueue(newsContentRequest);

        return view;
    }
}
